﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class OrderDetailsDA : BaseDAL, IOrderDetails
    {
        private const string tableName = "OrderDetails";
        private const string orderID = "OrderID";
        private const string bouquetID = "BouquetID";
        private const string quantity = "Quantity";
        private const string currentPrice = "CurrentPrice";

        public OrderDetailsDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        #region IOrderDetails Members

        public OrderDetails[] GetOrderDetails(int _orderID)
        {
            OrderDetails[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                    tableName, orderID, orderID);
                cmd.Parameters.Add(String.Format("@{0}", orderID), SqlDbType.Int).Value = _orderID;

                String[] columnNames = {    orderID,
                                            bouquetID,
                                            quantity,
                                            currentPrice };
                result = SelectCollection<OrderDetails>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public OrderDetails GetOrderDetails(int _orderID, int _bouquetID)
        {
            OrderDetails[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = @{4}",
                    tableName, orderID, orderID, bouquetID, bouquetID);
                cmd.Parameters.Add(String.Format("@{0}", orderID), SqlDbType.Int).Value = _orderID;
                cmd.Parameters.Add(String.Format("@{0}", bouquetID), SqlDbType.Int).Value = _bouquetID;

                String[] columnNames = {    orderID,
                                            bouquetID,
                                            quantity,
                                            currentPrice };
                result = SelectCollection<OrderDetails>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        public int InsertOrderDetails(OrderDetails _orderDetails, out int autoNumber)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    orderID,
                                            bouquetID,
                                            quantity,
                                            currentPrice};

                Object[] values = { _orderDetails.OrderID,
                                    _orderDetails.BouquetID,
                                    _orderDetails.Quantity,
                                    _orderDetails.CurrentPrice };

                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}